
// 打开App
function open_app(appName) {
    var name = getPackageName(appName);
    if (name) {
        toastLog("打开" + appName + "...");
        launch(name);
    }
    else {
        toastLog("App名称错误");
    }
    return name;
}

// 等待View存在并延时点击
function wait_view_exists_click(viewName, delayTime, showMsg) {
    while (true) {
        if (className("android.view.View").desc(viewName).exists()) {
            sleep(delayTime);
            toastLog("点击" + showMsg)
            className("android.view.View").desc(viewName).findOnce().click();
            break;
        }
    }
}

// 等待TextView存在并延时点击
function wait_textview_exists_click(textViewName, delayTime, showMsg) {
    while (true) {
        if (className("android.widget.TextView").text(textViewName).exists()) {
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.TextView").text(textViewName).findOnce().bounds();
            click(b.centerX(), b.centerY());
            break;
        }
    }
}

// 等待TextView存在并延时点击, 如果满足条件退出循环
function wait_textview_exists_click_break(textViewName, delayTime, showMsg, textViewNameBreak, showMsgBreak) {
    while (true) {
        if (className("android.widget.TextView").text(textViewName).exists()) {
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.TextView").text(textViewName).findOnce().bounds();
            click(b.centerX(), b.centerY());
            break;
        }
        else if (className("android.widget.TextView").text(textViewNameBreak).exists()) {
            toastLog(showMsgBreak)
            break;
        }
    }
}

// 等待Button存在并点击
function wait_button_exists_click(buttonName, delayTime, showMsg) {
    while (true) {
        if (className("android.widget.Button").text(buttonName).exists()) {
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.Button").text(buttonName).findOnce().bounds();
            click(b.centerX(), b.centerY());
            break;
        }
    }
}

// 京东抢购茅台预约
function jd_mt_yy() {
    var name = open_app("京东");
    waitForPackage(name);

    wait_view_exists_click("我的", 3000, "\"我的\"");
    wait_textview_exists_click("商品收藏", 3000, "\"商品收藏\"");
    wait_textview_exists_click("¥1499.00", 3000, "\"飞天茅台\"");
    wait_textview_exists_click_break("立即预约", 3000, "\"立即预约\"", "已预约", "预约成功");
    // wait_textview_exists_click_break("等待预约", 3000, "\"立即预约\"", "已预约", "预约成功");
    wait_button_exists_click("知道啦", 3000, "\"知道啦\"");

    toastLog("预约成功")
}

// 京东抢购茅台预约
function jd_mt_qg() {
    wait_textview_exists_click("立即抢购", 0, "\"立即抢购\"");
    wait_button_exists_click("提交订单", 0, "\"提交订单\"");
}

// 定时预约抢购
function timing_jd_mt(hour_yy, min_yy, sec_yy, hour_qg, min_qg, sec_qg) {
    var yy_flag = 0;    //预约成功标志
    toastLog("执行京东抢购任务...");
    while (true) {
        var nowDate = new Date();
        if (nowDate.getHours() == hour_yy && nowDate.getMinutes() == min_yy && nowDate.getSeconds() == sec_yy) {
            toastLog("定时时间到, 开始预约...");
            jd_mt_yy();
            yy_flag = 1;
        }
        if (nowDate.getHours() >= hour_qg && nowDate.getMinutes() >= min_qg && nowDate.getSeconds() >= sec_qg && yy_flag) {
            toastLog("定时时间到, 开始抢购...");
            jd_mt_qg();
        }
    }
}

jd_mt_yy();
// jd_mt_yy();
// timing_jd_mt(10, 02, 00, 12, 00, 00);